package com.uuah.server.config.spring.parsers.specific;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.uuah.api.lifecycle.IDisposable;
import com.uuah.api.lifecycle.Initialisable;
import com.uuah.config.object.AbstractObjectFactory;
import com.uuah.config.object.PrototypeObjectFactory;
import com.uuah.server.config.spring.parsers.AbstractUuahBeanDefinitionParser;

public class ShortcutComponentDefinitionParser extends
		ComponentDefinitionParser {

	private static Class OBJECT_FACTORY_TYPE = PrototypeObjectFactory.class;

	public ShortcutComponentDefinitionParser(Class clazz) {
		super(clazz);
	}

	// @Override
	protected void parseChild(Element element, ParserContext parserContext,
			BeanDefinitionBuilder builder) {
		Element parent = (Element) element.getParentNode();
		String serviceName = parent.getAttribute(ATTRIBUTE_NAME);
		builder.addPropertyReference("service", serviceName);

		String className = element.getAttributeNode(
				AbstractUuahBeanDefinitionParser.ATTRIBUTE_CLASS).getValue();

		GenericBeanDefinition objectFactoryBeanDefinition = new GenericBeanDefinition();
		objectFactoryBeanDefinition.setBeanClass(OBJECT_FACTORY_TYPE);
		objectFactoryBeanDefinition.getPropertyValues().addPropertyValue(
				AbstractObjectFactory.ATTRIBUTE_OBJECT_CLASS_NAME, className);
		objectFactoryBeanDefinition.setInitMethodName(Initialisable.PHASE_NAME);
		objectFactoryBeanDefinition.setDestroyMethodName(IDisposable.PHASE_NAME);

		builder.addPropertyValue("objectFactory", objectFactoryBeanDefinition);
		super.parseChild(element, parserContext, builder);
	}
}
